import { Alert } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from '../../util/http';

export const getUserInfo = async () => {
    try {
        const res = await axios.get('/user/info');
        await AsyncStorage.setItem('userInfo', JSON.stringify(res.data));
        return res.data;
    } catch (e) {
        Alert.alert('请求失败', e.message);
        return null;
    }
};
export const getUserInfoById = async id => {
    try {
        const res = await axios.get(`/user/info/${id}`);
        return res.data;
    } catch (e) {
        Alert.alert('请求失败', e.message);
        return null;
    }
};
/**
 *
 * @param {*} id mongoId
 * @param {*} data  'phone', 'avatar', 'nickname', 'desc', 'gender', 'birthday'
 * @returns
 */
export const updateUserById = async (id, data) => {
    try {
        const res = await axios.post(`/user/update/${id}`, data);
        return res.data;
    } catch (e) {
        Alert.alert('请求失败', e.message);
        return null;
    }
};

export const userUploadAvatar = async formData => {
    try {
        const res = await axios.post('/user/upload/avatar', formData, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        });
        return res.data;
    } catch (e) {
        Alert.alert('请求失败', e.message);
        return null;
    }
};
